package com.aiju.cash.ui.account;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.AccountApi;
import com.aiju.cash.core.event.AccountEvent;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.ListDialog;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-29 11:55
 * @version: V1.0
 * @Description: 设置预算
 */
public class AccountBudgetActivity extends BaseTitleActivity {
    public static List<String> type = new ArrayList<>();

    static {
        type.add("年度预算");
        type.add("季度预算");
        type.add("月度预算");
        type.add("每日预算");
    }

    private SettingItemEditView moneyEditView;
    private SettingItemTextView typeTextView;

    private String accountId = "";//账本id
    private int budgetType = 3;//默认月
    private String budgetMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_budget);
        setCenterTitle("设置预算");
        setRightTitle("保存");

        accountId = getIntent().getStringExtra("accountId");
        budgetMoney = getIntent().getStringExtra("budgetMoney");
        if (getIntent().hasExtra("budgetType")) {
            budgetType = getIntent().getIntExtra("budgetType", 0);
        }

        moneyEditView = (SettingItemEditView) findViewById(R.id.set_account_money);
        moneyEditView.setEditable(true);
        final EditText editText = (EditText) moneyEditView.getRightView();
        editText.setSingleLine();
        if (!TextUtils.isEmpty(budgetMoney)) {
            editText.setText(String.valueOf(budgetMoney));
            editText.setSelection(budgetMoney.length());
        }
        editText.setHintTextColor(Color.parseColor("#bbbbbb"));
        editText.setHint("请输入预算金额");
        editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });

        typeTextView = (SettingItemTextView) findViewById(R.id.set_account_type);
        typeTextView.setRightText(type.get(budgetType - 1));
        typeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showType();
            }
        });
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String money = editText.getText().toString();
                if (TextUtils.isEmpty(money)) {
                    T.ToastShort("请输入预算金额");
                    return;
                }
                Double d = Double.valueOf(money);
                if (d < 0) {
                    T.ToastShort("预算金额必须大于0");
                    return;
                }
                if (d > 100000000) {
                    T.ToastShort("预算金额必须小于一亿");
                    return;
                }
                setbudget(accountId, String.valueOf(budgetType), money);
            }
        });
    }

    /**
     * 设置预算
     *
     * @param id
     * @param budgetType
     * @param budgetMoney
     */
    private void setbudget(String id, String budgetType, String budgetMoney) {
        new AccountApi(mDialogHelper).setBudget(id, budgetType, budgetMoney, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("设置预算成功");
                EventCenter.getInstance().post(new AccountEvent(0));
                finish();
            }
        });
    }

    /**
     * 选择预算类型
     */
    private void showType() {
        new ListDialog(this)
                .setLayoutParams()
                .setData(type)
                .setOnItemOnClick(new ListDialog.OnItemOnClick() {
                    @Override
                    public void click(String item, int position) {
                        typeTextView.setRightText(item);
                        budgetType = type.indexOf(item) + 1;
                    }
                }).show();
    }
}
